package com.syqc.custom

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import com.syqc.comlib.utils.copyText

import com.syqc.monitor.databinding.CarInfoTextViewBinding
import io.reactivex.rxjava3.core.Observable


class CarInfoTextView : LinearLayout {
    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(
        context,
        attrs,
        defStyle
    ) {
        initView(context, attrs)
    }

    private lateinit var binding: CarInfoTextViewBinding

    @SuppressLint("CustomViewStyleable")
    private fun initView(context: Context, attrs: AttributeSet?) {
        binding = CarInfoTextViewBinding.inflate(LayoutInflater.from(context), this, true)
        val params = LayoutParams(0, LayoutParams.MATCH_PARENT)
        params.weight = 3f
        binding.tvRight.layoutParams = params
        setOnLongClickListener {
            copyText("${binding.tvLeft.text.trim()} : ${binding.tvRight.text}", context)
            true
        }
    }

    fun setLeftTitle(leftText: String) {
        binding.tvLeft.text = leftText
    }

    fun setRightText(rightText: String) {
        binding.tvRight.text = rightText
    }

    fun setRightWeight(rightWeight: Float) {
        val params = LayoutParams(0, LayoutParams.MATCH_PARENT)
        params.weight = rightWeight
        binding.llRight.layoutParams = params
    }

    fun setRightTextColor(color: Int) {
        binding.tvRight.setTextColor(ContextCompat.getColor(context, color))
    }

    fun setNextInVisible(isVisible: Boolean) {
        binding.ivNext.isInvisible = !isVisible
    }

    fun setPhoneInVisible(isVisible: Boolean) {
        binding.ivPhone.isInvisible = !isVisible
    }


    fun setOnNextListener(listener: OnClickListener) {
        binding.ivNext.setOnClickListener(listener)
    }

    fun setOnPhoneListener(listener: OnClickListener) {
        binding.ivPhone.setOnClickListener(listener)
    }


    fun setEndGone(){
        binding.ivPhone.isVisible=false
        binding.ivNext.isVisible=false
    }

}